package com.yoi.SendAndRecv;

import com.rabbitmq.client.Channel;
import com.yoi.bean.Book;
import com.yoi.bean.Ordermessage;
import com.yoi.service.OrderService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
/**
 *
 * Version2  RabbitListener&RabbitHandler搭配使用
 */
@Component
//监听
/*@RabbitListener(bindings=@QueueBinding(value=@Queue(value="rabbtimq1-resource1-queue",arguments=@Argument(name="x-queue-type",value="quorum"),durable="true"),
        exchange=@Exchange(name="rabbitmq1-resource1-exchange",type="topic"), key="rabbitmq1.resource1.*"),containerFactory = "CustomerContainerFactory")*/
public class OrderRecv {

    @Autowired
    private OrderService orderService;
    //参数为Body
    //@RabbitHandler
    @RabbitListener(bindings=@QueueBinding(value=@Queue(value="rabbitmq1-resource1-queue",arguments=@Argument(name="x-queue-type",value="quorum"),durable="true"),
            exchange=@Exchange(name="rabbitmq1-resource1-exchange",type="topic"), key="rabbitmq.rabbitmq1.resource1.*"),containerFactory = "CustomerContainerFactory")
    public void onOrderMessageString(@Payload Book book, @Headers Map<String,Object> headers, Channel channel)throws Exception{
        //消费者操作
        System.out.println("----收到新订单----");
        System.out.println("这里是消费者1接收到的订单数据："+book.toString()+"-----");
        //System.out.println("订单ID:"+book.toString());
        Long deliveryTag=(Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //ACK手动签收操作
        channel.basicAck(deliveryTag,true);
        //生成订单
        SimpleDateFormat A=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Ordermessage order =new Ordermessage(book.getId(),book.getName(), "下单时间："+A.format(new Date())+";消费者：Recv1");
        orderService.insertOrder(order);
        System.out.println("----已生成的订单详情----"+order.toString());

    }
    //参数为Java对象（序列化）
   // @RabbitHandler
    /*@RabbitListener(bindings=@QueueBinding(value=@Queue(value="rabbitmq1-resource1-queue",arguments=@Argument(name="x-queue-type",value="quorum"),durable="true"),
            exchange=@Exchange(name="rabbitmq1-resource1-exchange",type="topic"), key="rabbitmq1.resource1.*"),containerFactory = "CustomerContainerFactory")  */
    public void onOrderMessage(@Payload Book book, @Headers Map<String,Object> headers, Channel channel)throws Exception{
        //消费者操作
        System.out.println("----收到申请，生成订单----");
        System.out.println("订单ID:"+book.toString());
        Long deliveryTag=(Long) headers.get(AmqpHeaders.DELIVERY_TAG);
        //ACK手动签收操作
        channel.basicAck(deliveryTag,true);
        //生成订单
        SimpleDateFormat A=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Ordermessage order =new Ordermessage(book.getId(),book.getName(), "下单时间："+A.format(new Date()));
        orderService.insertOrder(order);
        System.out.println("----已生成的订单详情----"+order.toString());

}










    /*@RabbitHandler
    public void processMessage2(String message) {
        System.out.println(new String(message));
    }*/

    /*@RabbitListener(queues = "order-queue")
    public void onMessage(Message Message, Channel channel) throws Exception {
        System.out.println("Message content : " + Message);
        channel.basicAck(Message.getMessageProperties().getDeliveryTag(),false);
        System.out.println("消息已确认");
    }

    //监听多个队列
    @RabbitListener(queues = { "queue1", "queue2" })
    public void onMessageMore(Message message, Channel channel) throws Exception {
        System.out.println("Message content : " + message);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        System.out.println("消息已确认");
    }*/


}
